package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.PageResponseResult;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentPageRequest;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;

    //学员列表查询
    @Override
    public PageResponseResult<Student> queryAllStudent(StudentPageRequest studentPageRequest) {
//        1.传入分页查询所需要的参数
        PageHelper.startPage(studentPageRequest.getPage(),studentPageRequest.getPageSize());

//        2.调用mapper层的方法
        List<Student> studentList = studentMapper.queryAllStudent(studentPageRequest);

//        3.解析返回的结果
        Page<Student> page = (Page<Student>) studentList;

//        4.封装结果返回controller层
        return new PageResponseResult<>(page.getTotal(), page.getResult());

    }

    //添加学员
    @Override
    public void insertStudent(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.insertStudent(student);
    }

    /*修改学员信息,1.根据id查询回显*/
    @Override
    public Student queryByIdStudent(Integer id) {
        return studentMapper.queryByIdStudent(id);
    }

    /*修改学员信息,2.修改学员信息*/
    @Override
    public void updateStudent(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.updateStudent(student);
    }

    //违纪处理
    @Override
    public void violationStudent(Integer id, Integer score) {
        Student student = studentMapper.queryByIdStudent(id);
        student.setViolationCount(student.getViolationCount()+1);
        student.setViolationScore(student.getViolationScore()+score);
        studentMapper.updateStudent(student);
    }

    //删除学员
    @Override
    public void deleteStudent(List<Integer> ids) {
        studentMapper.deleteStudent(ids);
    }
}
